import 'package:boss/models/role.dart';
import 'package:boss/ui/page/role/page/roleDetail/provider/role_detail_provider.dart';
import 'package:boss/ui/page/role/provider/role_provider.dart';
import 'package:boss/ui/widget/alert/alert.dart';
import 'package:boss/ui/widget/back_confirmation.dart';
import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:boss/ui/widget/loading/loading.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:hi_common/hi_common.dart';
import 'package:provider/provider.dart';
import 'package:router/annotation.dart';

import 'widget/role_detail_browser.dart';

@RouterConfig(RoleDetailPage.pageName)
class RoleDetailPage extends StatefulWidget {
  static const String pageName = '/roleDetailPage';

  @Autowired()
  final Role role;

  const RoleDetailPage({
    this.role,
    Key key,
  }) : super(key: key);

  @override
  _RoleDetailPageState createState() => _RoleDetailPageState('职位管理');
}

class _RoleDetailPageState extends BaseState<RoleDetailPage> {
  _RoleDetailPageState(String pageName) : super(pageName);

  @override
  void initState() {
    super.initState();
    Provider.of<RoleDetailProvider>(context, listen: false).setRole(widget.role);

    SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
      init();
    });
  }

  init() async {
    showLoadingToast();
    Provider.of<RoleDetailProvider>(context, listen: false).queryPerms();
    hideLoadingToast();
  }

  @override
  Widget build(BuildContext context) {
    return BackConfirmation(
      backConfirmationText: '职位还未完成确认返回么？',
      child: Scaffold(
        appBar: CenterTitleAppBar(
          title: '职位管理',
          backConfirmationText: '职位还未完成确认返回么？',
          actions: <Widget>[
            if (widget.role != null)
              LabelButton(
                title: '删除',
                padding: EdgeInsets.only(left: 8, top: 2, bottom: 2),
                onTap: () => Provider.of<RoleProvider>(context, listen: false).deleteRole(widget.role),
              )
          ],
        ),
        body: RoleDetailBrowser(),
      ),
    );
  }
}
